<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\TrainingSession;
use Illuminate\Http\Request;

class TrainingReviewController extends Controller
{
    /**
     * 获取训练复盘信息
     */
    public function show(TrainingSession $session)
    {
        // 确保会话属于当前用户
        if ($session->user_id !== auth()->id()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        // 确保会话已完成
        if ($session->status !== 'completed') {
            return response()->json(['error' => 'Session not completed'], 400);
        }

        $session->load(['messages', 'character', 'scenario']);

        return response()->json([
            'session' => $session,
            'analysis' => [
                'total_messages' => $session->messages->count(),
                'duration' => $session->completed_at ? 
                    $session->completed_at->diffInMinutes($session->created_at) : 0,
                'performance_score' => $session->performance_score,
                'feedback' => $session->feedback
            ]
        ]);
    }

    /**
     * 重新开始类似的训练
     */
    public function replay(TrainingSession $session)
    {
        // 确保会话属于当前用户
        if ($session->user_id !== auth()->id()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        // 创建新的训练会话
        $newSession = TrainingSession::create([
            'user_id' => auth()->id(),
            'ai_character_id' => $session->ai_character_id,
            'training_scenario_id' => $session->training_scenario_id,
            'status' => 'active'
        ]);

        $newSession->load(['character', 'scenario']);

        return response()->json([
            'session' => $newSession,
            'message' => 'New training session created'
        ]);
    }
}